package com.ly.service.impl;

import com.ly.dao.UserCaseMapper;
import com.ly.pojo.domain.UserCaseDO;
import com.ly.pojo.dto.UserCaseDTO;
import com.ly.pojo.vo.PageResult;
import com.ly.service.UserCaseService;
import java.lang.Boolean;
import java.lang.Integer;
import java.lang.Object;
import java.lang.Override;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.multipart.MultipartFile;
import com.ly.exception.BusinessException;
import java.util.List;
import org.springframework.web.context.request.ServletRequestAttributes;
import java.util.Map;
import java.io.File;
import com.ly.constant.ResultCodeConstant;
import java.io.IOException;
import java.util.Date;

/**
 * 用户案例管理的实现
 *
 * @author Administrator
 * @date 2025-03-07 10:51:13
 */
@Service
public class UserCaseServiceImpl implements UserCaseService {

    @Autowired
    private UserCaseMapper userCaseMapper;

    @Override
    public Boolean addUserCase(UserCaseDTO userCaseDTO) {
        if (userCaseMapper.selectByTitle(userCaseDTO.getTitle()) != null) {
            throw new BusinessException(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG);
        }
        UserCaseDO userCaseDO = new UserCaseDO();
        userCaseDO.setTitle(userCaseDTO.getTitle());
        userCaseDO.setContent(userCaseDTO.getContent());
        userCaseDO.setImageUrl(userCaseDTO.getImageUrl());
        userCaseDO.setCustomerName(userCaseDTO.getCustomerName());
        userCaseDO.setCreateBy("admin");
        userCaseDO.setCreateTime(new Date());
        int result = userCaseMapper.insert(userCaseDO);
        return result > 0;
    }

    @Override
    public Boolean updateUserCase(UserCaseDTO userCaseDTO) {
        UserCaseDO existingUserCase = userCaseMapper.selectById(userCaseDTO.getId());
        if (existingUserCase == null) {
            throw new BusinessException(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG);
        }
        existingUserCase.setTitle(userCaseDTO.getTitle());
        existingUserCase.setContent(userCaseDTO.getContent());
        existingUserCase.setImageUrl(userCaseDTO.getImageUrl());
        existingUserCase.setCustomerName(userCaseDTO.getCustomerName());
        existingUserCase.setUpdateBy("admin");
        existingUserCase.setUpdateTime(new Date());
        int result = userCaseMapper.updateById(existingUserCase);
        return result > 0;
    }

    @Override
    public Boolean deleteUserCase(UserCaseDTO userCaseDTO) {
        UserCaseDO existingUserCase = userCaseMapper.selectById(userCaseDTO.getId());
        if (existingUserCase == null) {
            throw new BusinessException(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG);
        }
        int result = userCaseMapper.deleteById(userCaseDTO.getId());
        return result > 0;
    }

    @Override
    public PageResult<UserCaseDO> listUserCases(Integer pageIndex, Integer pageSize) {
        int start = (pageIndex - 1) * pageSize;
        List<UserCaseDO> userCases = userCaseMapper.selectAll(start, pageSize);
        long totalRecords = userCaseMapper.countAll();
        long totalPages = totalRecords / pageSize + (totalRecords % pageSize > 0L ? 1L : 0L);
        return new PageResult<>(totalRecords, totalPages, pageIndex.longValue(), pageSize.longValue(), userCases);
    }

    @Override
    public Boolean uploadUserCaseImage(Integer caseId, MultipartFile file, HttpServletRequest request) {
        String uploadDir = request.getServletContext().getRealPath("/uploads/");
        File directory = new File(uploadDir);
        if (!directory.exists()) {
            directory.mkdirs();
        }
        String filePath = uploadDir + file.getOriginalFilename();
        try {
            file.transferTo(new File(filePath));
        } catch (IOException e) {
            e.printStackTrace();
            throw new BusinessException(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG);
        }
        UserCaseDO userCaseDO = userCaseMapper.selectById(caseId);
        if (userCaseDO == null) {
            throw new BusinessException(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG);
        }
        userCaseDO.setImageUrl(filePath);
        userCaseDO.setUpdateBy("admin");
        userCaseDO.setUpdateTime(new Date());
        int result = userCaseMapper.updateById(userCaseDO);
        return result > 0;
    }
}
